<template>
  <a-drawer
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <!-- <a-form-model-item label="货区编号" prop="Code">
          <a-input v-model="entity.Code" :disabled="$para('GenerateStorAreaCode')=='1'" :placeholder="$para('GenerateStorAreaCode')=='1'?'系统自动生成':'货区编号'" autocomplete="off" />
        </a-form-model-item> -->
        <a-form-model-item label="库区编号" prop="Code">
          <a-input v-model="entity.Code" disabled placeholder="系统自动生成" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="库区名称" prop="Name">
          <a-input v-model="entity.Name" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="仓库" prop="StorId">
          <a-tree-select
            allowClear
            :treeData="DeptList"
            placeholder="请选择隶属机构"
            v-model="entity.StorId"
          ></a-tree-select>
        </a-form-model-item>
        <a-form-model-item label="货区类型" prop="Type">
          <enum-select code="StorAreaType" v-model="entity.Type"> </enum-select>
          <a-select v-model="entity.Type" allowClear >
            <a-select-option
              v-for="(item, index) in AreaTypeList"
              :value="item.Id"
              :key="index"
            >{{ item.DicName }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="库区纬度" prop="Latitude">
          <a-input v-model="entity.Latitude" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="库区经度" prop="Longitude">
          <a-input v-model="entity.Longitude" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="库区长度" prop="ReservoirLongs">
          <a-input v-model="entity.ReservoirLongs" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="库区宽度" prop="Reservoirwidths">
          <a-input v-model="entity.Reservoirwidths" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="库区高度" prop="Reservoirheight">
          <a-input v-model="entity.Reservoirheight" autocomplete="off" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <a-button
        :style="{ marginRight: '8px' }"
        @click="
          () => {
            this.visible = false
          }
        "
        >取消</a-button
      >
      <a-button type="primary" @click="handleSubmit">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>

export default {
  components: {
  },
  props: {
    parentObj: Object
  },
  data() {
    return {
      DeptList: [],
      AreaTypeList: [],
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {
        Name: [{ required: true, message: '请输入货区名称', trigger: 'blur' }],
        Type: [{ required: true, message: '请选择货区类型', trigger: 'change' }]
      },
      title: '',
      ParentIdTreeData: []
    }
  },
  methods: {
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.DeptList = []
      this.$http.post('/Base_Manage/Base_Department/GetTreeDataList', { }).then(resJson => {
        this.loading = false
        this.DeptList = resJson.Data
      })
      this.AreaTypeList = []
      this.$http.post('/Base_BasicData/Base_DictionaryMain/GetTheDataByDicKey', {
        Condition: 'DicKey',
        Keyword: 'StorAreaType'
      }).then(resJson => {
        this.loading = false
        this.AreaTypeList = resJson.Data.DictionaryDetailsList
      })
    },
    openForm(id, title) {
      this.init()
      // this.SelectOrgan()
      this.title = title
      if (this.title == '新增库区') {
        this.organ = ''
      }
      if (id) {
        this.loading = true
        this.$http.post('/PB/PB_StorArea/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
          this.organ = resJson.Data.StorId
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/PB/PB_StorArea/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
    DataTypeChange(val, option) {
      this.DataType = val
    }
  }
}
</script>
